/* +----------------------------------------------------------------------
 * | 食色内页JS [ 食色・分享味蕾的快乐 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2014 http://shiseapp.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: Gavin Foo <fuxiaopang@msn.com>
 * +----------------------------------------------------------------------
 */

function minidown(){
  window.open('http://shiseapp.com/go/s1tnsg9');
}

function gogetapp(){
  $('.gogetapp').fadeIn();
}

function hidegogetapp(){
  $('.gogetapp').fadeOut();
}

function gotopbutton(){
  $(window).scroll(function(){  
    var scrollt = document.documentElement.scrollTop + document.body.scrollTop; 
    if( scrollt >110 ){  
      $(".shise-app").fadeIn(400); 
    }else{      
      $(".shise-app").stop().fadeOut(400);   
    }
  });
};

var APIHOST = "http://shiseapp.com/ajax/";

function is_weixin(){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=="micromessenger") {
		return true;
 	} else {
		gogetapp();
	}
};

$(".jquery-on").on("click", '.mediaUsefulApi',function(){
  if(is_weixin()) {
    var $this = $(this);
    var selected = $this.data('selected');
    var count = $this.data('count');
    var api = $this.data('api');
    $this.html('······');
    var on = (selected)?'&on=0':'&on=1';
    $.post(APIHOST,{'api':api + on},function(result){
      if(result['nouuid']) window.location.href = "http://shiseapp.com/wx/login/?redirect_url=" + window.location.href;
      if(!result['succ']) return;
      if(selected){
        $this.html('<i class="icon-useful"></i>有用<span>' + (count-1) + '</span>');
        $this.data('selected',0);
        $this.data('count',count-1);
      } else {
        $this.html('<i class="icon-usefuled"></i>有用<span>'+ (count+1) +'</span>');
        $this.data('selected',1);
        $this.data('count',count+1);
      }
      },'json');
  }
});

$(".jquery-on").on("click", '.mediaLikeApi',function(){
  if(is_weixin()) {
    var $this = $(this);
    var user = $this.data('user');
    var liked = $this.data('liked');
    var count = $this.data('count');
    var likeApi = $this.data('likeapi');
    var unlikeApi = $this.data('unlikeapi');
    var api = (liked)?unlikeApi:likeApi;
    $this.html('······');
    $.post(APIHOST,{'api':api},function(result){
      if(result['nouuid']) window.location.href = "http://shiseapp.com/wx/login/?redirect_url=" + window.location.href;
      if(!result['succ']) return;
      if(liked){
        $this.html('<i class="icon-like"></i>赞<span>' + (count-1) + '</span>');
        $this.data('liked',0);
        $this.data('count',count-1);
      } else {
        $this.next().fadeIn();
        $this.html('<i class="icon-liked"></i><span class="liked">已赞</span><span>'+ (count+1) +'</span>');
        $this.data('liked',1);
        $this.data('count',count+1);
      }
      },'json');
  }
});

$(".jquery-on").on("click", '.likeApi',function(){
  if(is_weixin()) {
    var $this = $(this);
    var user = $this.data('user');
    var liked = $this.data('liked');
    var likeApi = $this.data('likeapi');
    var unlikeApi = $this.data('unlikeapi');
    var api = (liked)?unlikeApi:likeApi;
    $this.find('.num').html('···');
    $.post(APIHOST,{'api':api},function(result){
      if(result['nouuid']) window.location.href = "http://shiseapp.com/wx/login/?redirect_url=" + window.location.href;
      if(!result['succ']) return;
      if(liked){
        $this.next().fadeOut();
        $this.nextAll('#'+user).fadeOut();
        $this.find('.num').html('<span class="glyphicon glyphicon-heart">');
        $this.data('liked',0);
      } else {
        $this.next().fadeIn();
        $this.find('.num').html('已赞');
        $this.data('liked',1);
      }
      },'json');
  }
});